capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B14
*/

local data "" ;
local output "" ;

foreach gp in all phys nonphys othmen intel { ;
	use "prwora_bw264_crime_poly1allcov_iv_`gp'.dta", clear ;
	keep if 
		(
		variables == "n_crime2char_1997_2017" | 
		variables == "n_crime2chart_1997_2017" |
		variables == "n_crime2genr_1997_2017"	| 
		variables == "n_crime2genrt_1997_2017"	|
		variables == "n_crime2nogn_1997_2017" | 
		variables == "n_crime2nognt_1997_2017" | 
		variables == "n_crime2prtf_1997_2017" | 
		variables == "n_crime2prtft_1997_2017" | 
		variables == "n_crime2vrob_1997_2017" | 
		variables == "n_crime2vrobt_1997_2017" | 
		variables == "n_crime2vnrb_1997_2017" | 
		variables == "n_crime2vnrbt_1997_2017" | 
		variables == "n_crime2anti_1997_2017" | 
		variables == "n_crime2antit_1997_2017" | 
		variables == "n_crime2vcps_1997_2017" | 
		variables == "n_crime2vcpst_1997_2017" | 
		variables == "n_crime2prbg_1997_2017" | 
		variables == "n_crime2prbgt_1997_2017" | 
		variables == "n_crime2dgsl_1997_2017" | 
		variables == "n_crime2dgslt_1997_2017" | 
		variables == "n_crime2prfd_1997_2017" | 
		variables == "n_crime2prfdt_1997_2017" | 
		variables == "n_crime2niwp_1997_2017" | 
		variables == "n_crime2niwpt_1997_2017" | 
		variables == "n_crime2hinc_1997_2017" |
		variables == "n_crime2shinc_1997_2017" | 
		variables == "n_crime2ahinc_1997_2017" | 
		variables == "avg_kidemp15_1997_2017")
		& sample == "age18unfav" ;
	foreach stub in 
		char chart genr genrt nogn nognt prtf prtft vrob vrobt vnrb vnrbt anti antit
		vcps vcpst prbg prbgt dgsl dgslt prfd prfdt niwp niwpt hinc shinc ahinc { ;
			replace variables = "age18rd_`stub'"   if variables == "n_crime2`stub'_1997_2017" ;
	} ;
		replace variables = "age18rd_kidemp15" if variables == "avg_kidemp15_1997_2017" ;
	rename n_age18unfav b_endo ;
	rename sen_age18unfav se_endo ;
	keep variables b_endo se_endo mean_rd ;
	gen gp = "`gp'" ;
	gen exp = "age18rd" ;
	tempfile `gp' ;
	save ``gp'' ;
} ;

/* Diagnosis */
local diag1 "nonment" ;
local diag2 "othmen" ;
local diag3 "phys" ;
local diag4 "intel" ;
local diag5 "nonphys" ;
local diag1_label "Physical or ID" ;
local diag2_label "Other mental" ;
local diag3_label "Physical" ;
local diag4_label "Intellectual" ;
local diag5_label "All mental" ;
local diag1_color "green" ;
local diag2_color "orange" ;
local diag3_color "green" ;
local diag4_color "teal" ;

use `all', clear ;		 
append using ``diag2'' ;
append using ``diag3'' ;
append using ``diag4'' ;
append using ``diag5'' ;

gen treat_lvl = mean_rd + b_endo ;

gen lo95 = treat_lvl - 1.96*se_endo ;
gen hi95 = treat_lvl + 1.96*se_endo ;


/* Any */
capture drop sort ;
gen sort = . ;

foreach gp in nonphys phys { ;

	replace	sort = 12 + `add_`gp''  if variables == "age18rd_prtf" 	& gp == "`gp'" ;
	replace	sort = 10 + `add_`gp''  if variables == "age18rd_vcps" 	& gp == "`gp'" ;
	replace	sort = 8  + `add_`gp''  if variables == "age18rd_prfd" 	& gp == "`gp'" ;
	replace	sort = 6  + `add_`gp''  if variables == "age18rd_vrob" 	& gp == "`gp'" ;
	replace	sort = 4  + `add_`gp''  if variables == "age18rd_prbg" 	& gp == "`gp'" ;
	replace	sort = 2  + `add_`gp''  if variables == "age18rd_dgsl" 	& gp == "`gp'" ;
	replace	sort = 0  + `add_`gp''  if variables == "age18rd_anti" 	& gp == "`gp'" ;
} ;

twoway 

		(rbar hi95 lo95	sort 	if variables == "age18rd_prtf" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrob" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcps" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbg" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgsl" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfd" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_anti" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		
		(rbar hi95 lo95	sort 	if variables == "age18rd_prtf" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrob" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcps" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbg" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgsl" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfd" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_anti" & gp == "phys", color(green*.25) barwidth(.2) horizontal)

		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtf" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrob" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcps" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbg" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgsl" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfd" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_anti" & gp == "nonphys", color(orange) lwidth(medthin))
		
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtf" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrob" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcps" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbg" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgsl" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfd" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_anti" & gp == "phys", color(green) lwidth(medthin))
	
		(scatter sort mean_rd  	if variables == "age18rd_prtf" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_vrob" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_vcps" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_prbg" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_dgsl" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_prfd" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_anti" & gp == "nonphys", color(orange) msymbol(X))
		
		(scatter sort mean_rd 	if variables == "age18rd_prtf" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_vrob" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_vcps" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_prbg" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_dgsl" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_prfd" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_anti" & gp == "phys", color(green) msymbol(S) msize(small))

		(scatter sort mean_rd if var == "kidem", lcolor(none) lwidth(medthick) yaxis(2))
		
		(pcarrowi 12.85 .04 12.5  .06  "", 	mstyle(none) lwidth(vthin) color(black)) /* For control mean label */
		(pcarrowi 12.5  .12 12.9  .17  "", 	mstyle(none) lwidth(vthin) color(black)) /* for IV estimate label */
		
		, 
		legend(col(3) row(1) order(36 "Physical" 29 "All mental (intel + other)"))
		xsc(r(0 .4)) xtitle("Increase in likelihood of charge", size(medsmall))
		xlab(0(.1).4)
		ysc(r(-0.5 13) axis(1)) 
		ysc(r(-0.5 13) axis(2)) 
		ytitle("", axis(1)) 
		ytitle("", axis(2))
		ylabel(
		   12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib" 0 "Violent+disorder", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(1) ) 
		ylabel(
			 12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib" 0 "Violent+disorder", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(2) ) 
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0) axis(1))
		ylabel(,nogrid angle(0) axis(2))
		ttext(13 .025 "Control" "mean", size(tiny))
		ttext(13 .19 "IV estimate", size(tiny))
		plotregion(margin(0 0 2 2))  
		;
		graph export "`output'/figB14_a.png", width(1000) height(750) replace ;
		

/* Count */
local add_nonphys 	= 0 ;
local add_phys 		= .4 ;

capture drop sort ;
gen sort = . ;

foreach gp in nonphys phys { ;

	replace	sort = 12 + `add_`gp''  if variables == "age18rd_prtft" 	& gp == "`gp'" ;
	replace	sort = 10 + `add_`gp''  if variables == "age18rd_vcpst" 	& gp == "`gp'" ;
	replace	sort = 8  + `add_`gp''  if variables == "age18rd_prfdt" 	& gp == "`gp'" ;
	replace	sort = 6  + `add_`gp''  if variables == "age18rd_vrobt" 	& gp == "`gp'" ;
	replace	sort = 4  + `add_`gp''  if variables == "age18rd_prbgt" 	& gp == "`gp'" ;
	replace	sort = 2  + `add_`gp''  if variables == "age18rd_dgslt" 	& gp == "`gp'" ;
	replace	sort = 0  + `add_`gp''  if variables == "age18rd_antit" 	& gp == "`gp'" ;
} ;

twoway 

		(rbar hi95 lo95	sort 	if variables == "age18rd_prtft" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrobt" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcpst" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbgt" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgslt" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfdt" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_antit" & gp == "nonphys", color(orange*.25) barwidth(.2) horizontal)
		
		(rbar hi95 lo95	sort 	if variables == "age18rd_prtft" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrobt" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcpst" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbgt" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgslt" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfdt" & gp == "phys", color(green*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_antit" & gp == "phys", color(green*.25) barwidth(.2) horizontal)

		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtft" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrobt" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcpst" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbgt" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgslt" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfdt" & gp == "nonphys", color(orange) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_antit" & gp == "nonphys", color(orange) lwidth(medthin))
		
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtft" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrobt" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcpst" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbgt" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgslt" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfdt" & gp == "phys", color(green) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_antit" & gp == "phys", color(green) lwidth(medthin))
	
		(scatter sort mean_rd  	if variables == "age18rd_prtft" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_vrobt" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_vcpst" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_prbgt" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_dgslt" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_prfdt" & gp == "nonphys", color(orange) msymbol(X))
		(scatter sort mean_rd  	if variables == "age18rd_antit" & gp == "nonphys", color(orange) msymbol(X))
		
		(scatter sort mean_rd 	if variables == "age18rd_prtft" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_vrobt" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_vcpst" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_prbgt" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_dgslt" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_prfdt" & gp == "phys", color(green) msymbol(S) msize(small))
		(scatter sort mean_rd 	if variables == "age18rd_antit" & gp == "phys", color(green) msymbol(S) msize(small))

		(scatter sort mean_rd if var == "kidem", lcolor(none) lwidth(medthick) yaxis(2))
		
		(pcarrowi 12.85 .11 12.5  .13  "", 	mstyle(none) lwidth(vthin) color(black)) /* For control mean label */
		(pcarrowi 12.5  .25 12.9  .3  "", 	mstyle(none) lwidth(vthin) color(black)) /* for IV estimate label */
		
		, 
		legend(col(3) row(1) order(36 "Physical" 29 "All mental (intel + other)"))
		xsc(r(0 1)) xtitle("Increase in number of charges", size(medsmall))
		xlab(0(.2)1)
		ysc(r(-0.5 13) axis(1)) 
		ysc(r(-0.5 13) axis(2)) 
		ytitle("", axis(1)) 
		ytitle("", axis(2))
		ylabel(
		   12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib" 0 "Violent+disorder", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(1) ) 
		ylabel(
			 12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib" 0 "Violent+disorder", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(2) ) 
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0) axis(1))
		ylabel(,nogrid angle(0) axis(2))
		ttext(13 .08 "Control" "mean", size(tiny))
		ttext(13 .36 "IV estimate", size(tiny))
		plotregion(margin(0 0 2 2))  
		;
		graph export "`output'/figB14_b.png", width(1000) height(750) replace ;
		

		
